package com.jcnet.bizaia;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.jcnet.bizaia.bean.MainVodBean;
import com.jcnet.bizaia.fragment.JCVideoViewFragment2;
import com.jcnet.bizaia.fragment.VodDetalFragment;

public class VodDetailActivity extends JCSimpleSherlockActivity {
	
	public static final String OPEN_MENU  = "OPEN_MENU";
	public static final String CLOSE_MENU  = "CLOSE_MENU";
	public static final String BIG_SCREEN  = "BIG_SCREEN";
	public static final String SMAIL_SCREEN  = "SMAIL_SCREEN";
	
	private MainVodBean mainVodBean ;
	
	private VodDetalFragment vodDetalFragment;
	
	private BroadcastReceiver broadcastReceiver;
	
	private JCVideoViewFragment2 jcVideoViewFragment2;
	
	private boolean isBigScreen  = false;
	
	private  LayoutParams layoutParams;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		
		super.onCreate(savedInstanceState);
		super.jcTitle = getString(R.string.vodDetail);
		super.initActionBar();
		setContentView(R.layout.simple_fragment_lay_no_video);
		
		mainVodBean = (MainVodBean) getIntent().getSerializableExtra("mainVodBean");
		
		vodDetalFragment = (VodDetalFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
		
		vodDetalFragment.startHttp(mainVodBean);
		
		jcVideoViewFragment2 = (JCVideoViewFragment2)getSupportFragmentManager().findFragmentById(R.id.jcVodView);
		
		layoutParams= jcVideoViewFragment2.getView().getLayoutParams();
		
	
		
		broadcastReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				
				if(intent.getAction()==OPEN_MENU){
					showAction();
				}
				if(intent.getAction()==CLOSE_MENU){
					hideAction();
				}
				if(intent.getAction()==BIG_SCREEN){
					setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
					getSupportFragmentManager().beginTransaction().hide(vodDetalFragment).commit();
					//layoutParams.height =LayoutParams.MATCH_PARENT;
					jcVideoViewFragment2.getView().setLayoutParams(layoutParams);
					isBigScreen  = true;
				}
				if(intent.getAction()==SMAIL_SCREEN){
					changeSmailScreen();
				}
			}

		
		};
		
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(OPEN_MENU);
		intentFilter.addAction(CLOSE_MENU);
		intentFilter.addAction(BIG_SCREEN);
		intentFilter.addAction(SMAIL_SCREEN);
		
		registerReceiver(broadcastReceiver, intentFilter);
		  
		
	
	}
	
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add("dlan")
        .setIcon(R.drawable.dlan_yes)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		return super.onCreateOptionsMenu(menu);
	}
	
	private void changeSmailScreen() {
		// TODO Auto-generated method stub
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
		getSupportFragmentManager().beginTransaction().show(vodDetalFragment).commit();
		jcVideoViewFragment2.getView().setLayoutParams(layoutParams);
		isBigScreen  = false;
	}
	
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			if(isBigScreen){
				changeSmailScreen();
				jcVideoViewFragment2.changeZoonButton();
			}else{
				finish();
			}
			break;

		default:
			break;
		}
		
		
		if(item.getTitle().equals("dlan")){
			sendBroadcast(new Intent(JCVideoViewFragment2.JC_DLAN_OK));
		}
		return true;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		unregisterReceiver(broadcastReceiver);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	
		// TODO Auto-generated method stub
		if(keyCode ==KeyEvent.KEYCODE_BACK){
			changeSmailScreen();
			jcVideoViewFragment2.changeZoonButton();
			
			return true;
		}
		if(keyCode==KeyEvent.KEYCODE_HOME){
			return true;
		}
		return true;
	}

}
